package com.sat.wisdom.Activities;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.sat.wisdom.R;
import com.sat.wisdom.Utils.Constant;
import com.sat.wisdom.Utils.SpUtil;

import java.util.ArrayList;

import static java.lang.System.exit;

public class GuideActivity extends Activity {

    private int[] imageId = {R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
    private ArrayList<ImageView> list;
    private int point_space;
    private LinearLayout ll_container;
    private ImageView iv_guide_red;

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ViewPager vp_guide = findViewById(R.id.vp_guide);
        final Button bt_start = findViewById(R.id.bt_start);
        ll_container = findViewById(R.id.ll_container);
        iv_guide_red = findViewById(R.id.iv_guide_red);

//        Window window = getWindow();
//        Window.OnFrameMetricsAvailableListener frameMetricsAvailableListener = new Window.OnFrameMetricsAvailableListener() {
//            @Override
//            public void onFrameMetricsAvailable(Window window, FrameMetrics frameMetrics, int i) {
//                long layout_measure_duration = frameMetrics.getMetric(FrameMetrics.LAYOUT_MEASURE_DURATION);
//                long draw_duration = frameMetrics.getMetric(FrameMetrics.DRAW_DURATION);
//                long total_duration = frameMetrics.getMetric(FrameMetrics.TOTAL_DURATION);
//                Log.d("TAG","total="+total_duration+"===layout_measure_duration="+layout_measure_duration+
//                "===draw_duration="+draw_duration);
//            }
//        };
//        Handler frameMetricsHandler = new Handler();
//        window.addOnFrameMetricsAvailableListener(
//                frameMetricsAvailableListener, frameMetricsHandler);
        initData();
        vp_guide.setAdapter(new MyPagerAdapter());
        bt_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);
                SpUtil.setBoolean(GuideActivity.this, Constant.GUIDE_SHOWN,true);
                finish();
                //exit(0);//释放图片所占内存
            }
        });

        vp_guide.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //开始滚动
                //String str = String.format("position=%d,offset=%f", position, positionOffset);
                //Log.d("TAG",str);
                float red_point_offset = positionOffset * point_space+position*point_space;
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_guide_red.getLayoutParams();
                params.leftMargin = (int) red_point_offset;
                iv_guide_red.setLayoutParams(params);
            }

            @Override
            public void onPageSelected(int position) {
                //切换完成
                if(position==list.size()-1){
                    bt_start.setVisibility(View.VISIBLE);
                }else {
                    bt_start.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //动2静(1)，静2动(2)，no thing to do(0)
                //Log.d("TAG","change，state="+state);
            }
        });

        ll_container.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ll_container.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                point_space = ll_container.getChildAt(1).getLeft() -
                        ll_container.getChildAt(0).getLeft();
                Log.d("TAG","point_space="+ point_space);
            }
        });

    }

    private void initData() {
        list = new ArrayList<ImageView>();
        for (int i=0;i<imageId.length;i++){
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(imageId[i]);
            list.add(imageView);
            ImageView grey_circle = new ImageView(this);
            grey_circle.setImageResource(R.drawable.shap_guide_grey);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            if (i>0){
                params.leftMargin = 30;
            }
            grey_circle.setLayoutParams(params);
            ll_container.addView(grey_circle);
        }
    }

    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = list.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
